package com.cuicui.mall.service;

import com.cuicui.mall.dao.LogDataMapper;
import com.cuicui.mall.entity.LogData;
import com.cuicui.mall.entity.LogDataExample;
import com.cuicui.mall.vo.LogVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LogService {
    @Autowired
    private LogDataMapper logDataDao;


    /**
     *
     * @param userName 用户
     * @param pageNo 分页
     * @param pageSize 尺寸
     * @return
     */
    public PageInfo<LogVO> getPage (String userName, Integer pageNo, Integer pageSize){
        LogDataExample logDataExample = new LogDataExample();
        LogDataExample.Criteria criteria = logDataExample.createCriteria();
        PageHelper.startPage(pageNo, pageSize);

        if(userName != null && !userName.equals("")){
            criteria.andUserNameLike(userName);
        }

        List<LogData> logData = logDataDao.selectByExample(logDataExample);
        List<LogVO> logVOList = logData.stream().map((logDataItem)->{
            LogVO logVO = new LogVO();
            BeanUtils.copyProperties(logDataItem, logVO);
            return logVO;
        }).collect(Collectors.toList());
        PageInfo<LogVO> pageInfo = new PageInfo<>();
        pageInfo.setList(logVOList);

        return pageInfo;
    }
}
